package com.sinoglobal.shakeprize.activity.prize;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.sinoglobal.shakeprize.R;
import com.sinoglobal.shakeprize.activity.base.AbstractActivity;
import com.sinoglobal.shakeprize.adapter.PrizeListAdapter;
import com.sinoglobal.shakeprize.bean.PrizeListParamerItem;
import com.sinoglobal.shakeprize.bean.ResponseUserPrizeList;
import com.sinoglobal.shakeprize.utils.Contstent;
import com.sinoglobal.shakeprize.utils.OKhttpUtil;
import com.sinoglobal.shakeprize.utils.OKhttpUtil.ResponseCallback;
import com.sinoglobal.shakeprize.utils.SPUtil;

import okhttp3.Request;

/**
 * @author temulu
 * @createdate 2015-8-26下午2:38:22
 * @Description 奖品中心界面
 */
public class MyPrizeActivity extends AbstractActivity implements
		OnClickListener {
	private LinearLayout prizeRemind;
	private TextView prizeRemindDes;
	private ListView prizeListView;
	private ResponseUserPrizeList userPrizeList;
	private List<PrizeListParamerItem> prizeList;
	private RelativeLayout rlNoprize;
	private LinearLayout llPrize;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prize_mine_activity);
		reLoadTv.setOnClickListener(this);
		prizeRemind = (LinearLayout) findViewById(R.id.prizeRemind);
		prizeRemindDes = (TextView) findViewById(R.id.prizeRemindDes);
		prizeListView = (ListView) findViewById(R.id.prizeList);
		rlNoprize = (RelativeLayout) findViewById(R.id.rl_noprize);
		llPrize = (LinearLayout) findViewById(R.id.ll_prize);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		prizeRemind.setVisibility(View.GONE);
	}

	private void initData() {
		titleView.setText("我的奖品");
		Map<String, Object> request=new HashMap<String, Object>();
		request.put("productCode", Contstent.PRODUCT_IDENTIFICATION);
		request.put("userId", SPUtil.getString(mActivity, Contstent.USER_ID));
		request.put("permissioncode", Contstent.MODULE_IDENTIFICATION);
		String json = JSON.toJSONString(request);
		String url = Contstent.PRIZE_PATH + "prize/prizeListByUId.do?json="
				+ json;
		OKhttpUtil.enqueue(url, new ResponseCallback() {

			@Override
			public void onFailure(Request paramRequest, IOException paramIOException) {
				showReLoading();
			}

			public void onResponse(String json) throws IOException {
				if (!TextUtils.isEmpty(json)) {
					try {
						userPrizeList = JSON.parseObject(json,
								ResponseUserPrizeList.class);
					} catch (Exception e) {
						Toast.makeText(MyPrizeActivity.this, "访问网络失败，请稍后再试", Toast.LENGTH_SHORT).show();
						return;
					}
					if (userPrizeList.rs.resultList.size() > 0) {
						prizeList = userPrizeList.rs.resultList;
						dismisLoading();
						initAllListView();
						unclaimed();
					} else {
						rlNoprize.setVisibility(View.VISIBLE);
						llPrize.setVisibility(View.GONE);
					}
				}
			}


		});
	}

	private void initAllListView() {
		prizeListView.setAdapter(new PrizeListAdapter(MyPrizeActivity.this,
				prizeList));
		prizeListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (2 == prizeList.get(position).pickstatus
						|| 4 == prizeList.get(position).pickstatus
						|| 5 == prizeList.get(position).pickstatus
						|| 6 == prizeList.get(position).pickstatus
						|| 7 == prizeList.get(position).pickstatus) {
					Intent intent = new Intent(MyPrizeActivity.this,
							MyPrizeDetailsActivity.class);
					intent.putExtra("accepttype",
							prizeList.get(position).accepttype);
					intent.putExtra("userprizeId",
							prizeList.get(position).userprizeid);
					intent.putExtra("prizeId", prizeList.get(position).prizeid);
					intent.putExtra("pickstatus",
							prizeList.get(position).pickstatus);
					intent.putExtra("roundid", prizeList.get(position).roundid);
					startActivity(intent);
				}
			}
		});
	}

	/**
	 * 获得即将过期未领取的奖品列表
	 */
	private void unclaimed() {
		Map<String, Object> request=new HashMap<String, Object>();
		request.put("productCode", Contstent.PRODUCT_IDENTIFICATION);
		request.put("userId", SPUtil.getString(mActivity, Contstent.USER_ID));
		request.put("permissioncode", Contstent.MODULE_IDENTIFICATION);
		String json = JSON.toJSONString(request);
		String url = Contstent.PRIZE_PATH + "prize/getNoRePrizeByUser.do?json="
				+ json;
		OKhttpUtil.enqueue(url, new ResponseCallback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {
				showReLoading();
			}

			@Override
			public void onResponse(String json) throws IOException {
				if (!TextUtils.isEmpty(json)) {
					try {
						userPrizeList = JSON.parseObject(json,
								ResponseUserPrizeList.class);
					} catch (Exception e) {
						Toast.makeText(MyPrizeActivity.this, "访问网络失败，请稍后再试", Toast.LENGTH_SHORT).show();
						return;
					}
					if (userPrizeList.rs.resultList!=null&&userPrizeList.rs.resultList.size()>0) {
						initWillExpireView();
					}
				}
			}
		});
	}


	/**
	 * 设置即将过期view
	 */
	protected void initWillExpireView() {
		prizeRemind.setVisibility(View.VISIBLE);
		prizeRemind.setOnClickListener(this);
		prizeRemindDes.setText("亲，您有" + userPrizeList.rs.resultList.size()
				+ "个即将过期的奖品未领取，点击领取~");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View paramView) {
		if (paramView.getId()==R.id.reLoadTv){// 重新加载
			initData();
		} else if (paramView.getId()==R.id.prizeRemind){
			Intent intent = new Intent(MyPrizeActivity.this,
					MyPrizeWillExpireActivity.class);
			intent.putParcelableArrayListExtra("willExpireList",
					(ArrayList<? extends Parcelable>) userPrizeList.rs.resultList);
			startActivity(intent);
		}
	}
}
